using System;
using DaaseVarmer.helperClasses.HardwareInterface;
using DaaseVarmer.domain;
using Microsoft.SPOT;

namespace DaaseVarmer.helperClasses
{
    class TemperatureSensorDriver : IDisposable
    {
        private static TemperatureSensorDriver instance;
        private TMP102 temperaturSensor;
        private int OUTSIDE_TEMP = 2;       //the tempratur selector where Add0 is connected to 3,3v
        private int SOLARHEATER_TEMP = 1;   //the tempratur selector where Add0 is connected to GND 


        public TemperatureSensorDriver()
        {
            if (temperaturSensor != null)
                temperaturSensor.Dispose();

            temperaturSensor = new TMP102();
        }

        public static TemperatureSensorDriver Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new TemperatureSensorDriver();
                }
                return instance;
            }
        }


        public int GetTemperatur(WriteType tempSensor)
        {
            switch (tempSensor)
            {
                case WriteType.TempOutside:
                    temperaturSensor.SelectTemperaturSensor(OUTSIDE_TEMP);
                    return int.Parse(temperaturSensor.GetTemperature());
                    break;

                case WriteType.TempSolarHeater:
                    temperaturSensor.SelectTemperaturSensor(SOLARHEATER_TEMP);
                    return int.Parse(temperaturSensor.GetTemperature());
                    break;

                default:
                    return -5001;
                    break;
                    
            }
        }

        public void Dispose()
        {
            if (temperaturSensor != null)
            {
                temperaturSensor.Dispose();
            }
           
        }


        public string WriteTypeEnumToString(WriteType writeType)
        {
            switch (writeType)
            {
                default:
                case WriteType.Error:
                    return "Error";
                    break;
                    ;
                case WriteType.Info:
                    return "info";
                    break;
                case WriteType.MotorSpeed:
                    return "Motorspeed";
                break;
                case WriteType.TempOutside:
                    return "TempOutside";
                    break;
                case WriteType.TempSolarHeater:
                    return "TempSolarHeater";
                    break;


            }
        }
    }

}
